package com.aw.baselib.util

/**
 * Created by hzw
 */
object AwBtnClickUtil {
    /**
     * 点击间隔
     */
    const val CLICK_INTERVAL: Long = 1000
    private var lastClickTime: Long = 0
    private var lastButtonId = -1
    val isFastDoubleClick: Boolean
        /**
         * 判断两次点击的间隔，如果小于指定值，则认为是多次无效点击
         *
         * @return
         */
        get() = isFastDoubleClick(-1, CLICK_INTERVAL)

    /**
     * 判断两次点击的间隔，如果小于指定值，则认为是多次无效点击
     *
     * @return
     */
    @JvmStatic
    fun isFastDoubleClick(buttonId: Int): Boolean {
        return isFastDoubleClick(buttonId, CLICK_INTERVAL)
    }

    /**
     * 判断两次点击的间隔，如果小于diff，则认为是多次无效点击
     *
     * @param diff
     * @return
     */
    fun isFastDoubleClick(buttonId: Int, diff: Long): Boolean {
        val time = System.currentTimeMillis()
        val timeD = time - lastClickTime
        if (lastButtonId == buttonId && lastClickTime > 0 && timeD < diff) {
            AwLog.d("按钮点击: 短时间内按钮多次触发, 不执行任何操作")
            return true
        }
        AwLog.d("按钮点击: 非短时间内按钮多次触发, 执行指定操作")
        lastClickTime = time
        lastButtonId = buttonId
        return false
    }
}